﻿/*
订单管理
*/

function OrderIndex()
{
    this.urlRoot = ctx + '/Order/';

    this.tableObj;

    var InitUi = function () {
        if (type != 0) {
            //只有未付款才显示付款按钮
            $('#btnPayment').hide();
        }
    }();
}


OrderIndex.prototype.initTable = function () {
    var This = this;
    var options = {
        url: This.urlRoot + "GetOrderList",
        cutHeight: 5,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        toolbar: "#exampleToolbar",
        queryParams: function (params) {
            params = {
                rows: params.limit,
                //页面大小  
                page: params.offset / params.limit + 1,
                orderCode: $('#orderCode').val(),
                statu : type
            }
            return params;
        },
        responseHandler: function (server) {
            if (server.Statu == 0) {
                return server.Data;
            } else {
                layer.alert('获取订单数据失败', { icon: 5 });
            }
        },
        columns: [
        {
            radio: true
        },
        {
            field: '',//第一列序号
            title: '序号',
            align: 'center',
            width: 50,
            formatter: function (value, row, index) {
                return index + 1;
            }
        },
        {
            field: 'orderCode',
            title: '订单编码',
            align: 'center',
            formatter: function (v, r, i) {
                return '<a href="#" class="orderCodeA">'+v+'</a>';
            }
        },
        {
            field: 'money',
            title: '应支付金额',
            align: 'center'
        },
        {
            field: 'pramoney',
            title: '实际支付金额',
            align: 'center',
            visible : type>0 ? true : false
        },
        {
            field: 'cTime',
            title: '创建时间',
            align: 'center'
        }, {
            field: 'hTime',
            title: '处理时间',
            align: 'center'
        }, {
            field: 'userName',
            title: '用户',
            align: 'center'
        }]
    }
    This.tableObj = tableHelper.initTable("exampleTableToolbar", options);
}

OrderIndex.prototype.reloadTable = function () {
    this.tableObj.bootstrapTable('refresh');
}

OrderIndex.prototype.getSelectRow = function () {
    var selectRows = this.tableObj.bootstrapTable('getSelections');
    if (selectRows.length == 0) {
        layer.alert('请选择一条订单', { icon: 6 });
        return null;
    }
    return selectRows[0];
}

OrderIndex.prototype.bind = function ()
{
    var This = this;

    $('#resetForm').click(function () {
        $('#orderCode').val('');
        This.reloadTable();
    });
    $('#btnSearch').click(function () {
        This.reloadTable();
    });

    $('#exampleTableToolbar').on('click', '.orderCodeA', function () {
        var code = $(this).text();
        layer.open({
            type: 2,
            title: '订单信息：'+code,
            shade: [0],
            anim: 2,
            area: ['100%', '100%'],
            content: '../Detailed/' + code
        });
    });

    $('#btnPayment').click(function () {
        var row = This.getSelectRow();
        if (row == null) {
            return;
        }
        //付款订单
        layer.open({
            content: '确认付款<span style="color:red;">'+row.orderCode+'</span>订单吗？',
            btn: ['付款', '取消'],
            icon: 3,
            yes: function (index, layero) {
                layer.prompt({ title: '请输入实际付款金额', formType: 0, value: row.money }, function (val, index) {
                    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
                    if (!reg.test(val)) {
                        layer.alert('金额格式不正确，正确格式：100、19.6', { icon: 5 });
                        return;
                    } else {
                        layer.close(index);
                        var PIndex = layer.msg('正在提交数据', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
                        $.post(This.urlRoot + 'Payment', { orderCode: row.orderCode, pramoney: val }, function (server) {
                            layer.close(PIndex);
                            if (server.Statu == 0) {
                                layer.msg('付款成功', { icon: 1 });
                                This.reloadTable();
                            } else {
                                layer.alert('付款失败', { icon: 5 });
                            }
                        });
                    }
                });
            }
        });
    });
}

$(function () {
    var shopping = new OrderIndex();
    shopping.bind();
    shopping.initTable();
});